package org.fto.jthink.ai.example.fg;

import java.util.*;

/**
 * 统计表
 * 
 * @author wenjian
 *
 */
public class Statistics {
  //FingerGuessingGame game;
  
  
  /* 玩家所有出拳 */
  //private List pactorHands=new ArrayList();
  /* 电脑所有出拳 */
  //private List aiactorHands=new ArrayList();
  /* 玩家和电脑所有出拳，以及输赢情况,Object[]{玩家出拳，电脑出拳，比赛结果},比赛结果是站在玩家角度来计算 */
  private List hands = new ArrayList(); 
  private int handSize=0;
  
  /* 玩家胜利次数 */
  private int pactorWinNums=0;
  /* 电脑胜利次数 */
  private int aiactorWinNums=0;
  /* 打成平手次数 */
  private int deuceNums=0;
  /* 玩家胜率 */
  private double pactorWinRatio=0.0;
  /* 电脑胜率 */
  private double aiactorWinRatio=0.0;
  /* 平手率 */
  private double deuceRatio=0.0;
  
  /**
   * 统计
   */
  public int stat(String phand, String aihand){
    int result = getResult(phand, aihand);
    hands.add(new Object[]{phand, aihand, new Integer(result)});
    handSize++;
    if(result==1){
      pactorWinNums++;
    }else if(result==-1){
      aiactorWinNums++;
    }else{
      deuceNums++;
    }
    pactorWinRatio = (double)pactorWinNums/(double)handSize;
    aiactorWinRatio = (double)aiactorWinNums/(double)handSize;
    deuceRatio = (double)deuceNums/(double)handSize;
    return result;
  }

  /**
   * 统计电脑角色最近连续n次的胜率
   * @param n
   * @return
   */
  public double statAIActorWinRatio(int n){
    //-1
    int winNums=0;
    int m=n;
    for(int i=hands.size()-1;i>=0 && m>0;m--,i--){
      //System.out.println("winNums:"+((double)winNums));
      Object[] hand = (Object[])hands.get(i);
      //System.out.println("hand[2]:"+(hand[2]));
      if(((Integer)hand[2]).intValue()==-1){//是电脑赢
        winNums++;
      }
    }
    
    //System.out.println("n:"+(n));
    //System.out.println("(double)winNums/(double)n:"+((double)winNums/(double)n));
    return (double)winNums/(double)n;
  }
  
  /**
   * 返回玩家样本数据
   * @param start 开始位置，从那里开始取样本
   * @param nums 样本数量
   * @param nums 样本长度
   */
  public List getSampleDatas(int nums, int len){
    List datas = new ArrayList();
    //int start = handSize-(nums*len)+1;
    int start = handSize-(nums+len-1);
    //System.out.println("样本开始位置:"+start);
    start = start<0?0:start;
    if(handSize<len){
      return datas;
    }
    //for(int i=0;i<nums && (i*len+len)<handSize ;i++){
    for(int i=0;i<nums && (i+len)<=handSize ;i++){
      double[] data = new double[len];
      for(int j=0;j<len;j++){
        //System.out.print(" start+("+i+")+"+j+":"+(start+(i)+j)+" ");
        Object[] hand = (Object[])hands.get(start+(i)+j);
        int pactorhand = Integer.parseInt((String)hand[0]);
        //System.out.print(pactorhand+"("+(Double.parseDouble(pactorhand)-1)/2+") ");
//        if(pactorhand==1){
//          data[j]=0.01;
//        }else if(pactorhand==2){
//          data[j]=0.5;
//        }else{
//          data[j]=0.99;
//        }
        data[j]=((double)(pactorhand)-1)/2;
        //System.out.print(pactorhand+"("+data[j]+") ");
        
      }
      //System.out.println();
      datas.add(data);
    }
    return datas;
  }
  
  public List getHands(){
    return hands;
  }
  public int getPlayerActorWinNums(){
    return pactorWinNums;
  }
  public int getAIActorWinNums(){
    return aiactorWinNums;
  }
  public int getDeuceNums(){
    return deuceNums;
  }
  public double getPlayerActorWinRatio(){
    return pactorWinRatio;
  }
  public double getAIActorWinRatio(){
    return aiactorWinRatio;
  }
  public double getDeuceRatio(){
    return deuceRatio;
  }
  
  /**
   * 返回比赛结果
   * 1=赢，0=平手，-1=输
   */
  private int getResult(String phand, String aihand){
    return Integer.parseInt((String)handTable.get(phand+"VS"+aihand));
  }
  /**
   * 构建输赢表,玩家VS电脑, 0平手，-1输，1赢
   */
  private static Map handTable = new HashMap();  
  static{
    handTable.put("1VS1", "0");
    handTable.put("1VS2", "1");
    handTable.put("1VS3", "-1");
    handTable.put("2VS1", "-1");
    handTable.put("2VS2", "0");
    handTable.put("2VS3", "1");
    handTable.put("3VS1", "1");
    handTable.put("3VS2", "-1");
    handTable.put("3VS3", "0");
  }
}
